package com.easyadmin.modules.stock.service.impl;


import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.modules.stock.domain.SysExpectPlan;
import com.easyadmin.modules.stock.domain.SysFuturesEntity;
import com.easyadmin.modules.stock.domain.SysStockPriceWarnEntity;
import com.easyadmin.modules.stock.domain.SysStockProductAgeEntity;
import com.easyadmin.modules.stock.mapper.SysExpectPlanDao;
import com.easyadmin.modules.stock.mapper.SysStockPriceWarnDao;
import com.easyadmin.modules.stock.mapper.SysStockProductAgeDao;
import com.easyadmin.modules.stock.service.SysStockPriceWarnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysStockPriceWarnService")
public class SysStockPriceWarnServiceImpl extends ServiceImpl<SysStockPriceWarnDao, SysStockPriceWarnEntity>
        implements SysStockPriceWarnService {


    @Autowired
    SysStockPriceWarnDao sysStockPriceWarnDao;
    @Autowired
    SysStockProductAgeDao sysStockProductAgeDao;
    @Autowired
    SysExpectPlanDao sysExpectPlanDao;

    @Autowired
    @Lazy
    private SysStockPriceWarnService sysStockPriceWarnService;





    @Override
    public PageInfo<SysStockPriceWarnEntity> queryPage(SysStockPriceWarnEntity sysStockPriceWarnEntity, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<SysStockPriceWarnEntity> data =sysStockPriceWarnDao.selectList(new QueryWrapper<SysStockPriceWarnEntity>().lambda()
                .eq(StringUtils.isNotBlank(sysStockPriceWarnEntity.getAuditStatus()),
                        SysStockPriceWarnEntity::getAuditStatus,sysStockPriceWarnEntity.getAuditStatus())
                .eq(StringUtils.isNotBlank(sysStockPriceWarnEntity.getProName()),
                        SysStockPriceWarnEntity::getProName,sysStockPriceWarnEntity.getProName())
                .orderBy(true,false,
                        SysStockPriceWarnEntity::getWarnTime,SysStockPriceWarnEntity::getProId));
        return new PageInfo<>(data);
    }

    @Override
    public PageInfo<SysStockPriceWarnEntity> aqueryPage(SysStockPriceWarnEntity sysStockPriceWarnEntity, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<SysStockPriceWarnEntity> data =sysStockPriceWarnDao.selectList(new QueryWrapper<SysStockPriceWarnEntity>().lambda()
                .eq(StringUtils.isNotBlank(sysStockPriceWarnEntity.getAuditStatus()),
                        SysStockPriceWarnEntity::getAuditStatus,"1"));
        return new PageInfo<>(data);
    }

    @Override
    public Boolean remove(String id) {
        sysStockPriceWarnDao.deleteById(Integer.valueOf(id));
        return true;
    }

    /**
     * Describe: 根据 id 批量删除数据
     * Param: ids
     * Return: Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemove(String[] ids) {
        for(String  id:ids){
            sysStockPriceWarnDao.deleteById(Integer.valueOf(id));
        }
        return true;
    }



    /**
     * Describe: 修改用户数据
     * Param: SysUser
     * Return: 操作结果
     */
    @Override
    public boolean update(SysStockPriceWarnEntity sysStockPriceWarnEntity) {
        if(!sysStockPriceWarnEntity.getAuditStatus().equals("0")){
            sysStockPriceWarnEntity.setAuditTime(new Date());
        }
        Integer result = sysStockPriceWarnDao.updateById(sysStockPriceWarnEntity);

        if(StringUtils.isNotBlank(sysStockPriceWarnEntity.getAuditStatus()) &&
                "1".equals(sysStockPriceWarnEntity.getAuditStatus())){
            SysStockPriceWarnEntity warnEntity=sysStockPriceWarnService.getById(sysStockPriceWarnEntity.getId());
            SysExpectPlan sysExpectPlan=new SysExpectPlan();
            sysExpectPlan.setProId(warnEntity.getProId());
            sysExpectPlan.setProName(warnEntity.getProName());
            sysExpectPlan.setWarnPrice(warnEntity.getWarnPrice());
            sysExpectPlan.setMarket(warnEntity.getMarket());
            sysExpectPlan.setCreateTime(LocalDateTime.now());
            sysExpectPlan.setWorkerFlag("0");
            sysExpectPlan.setWarnId(warnEntity.getId());
            sysExpectPlanDao.insert(sysExpectPlan);
        }
        return result > 0;
    }

    @Override
    public boolean synch(String id) {
        if(id.contains(",")){
            return false;
        }
        SysStockPriceWarnEntity entity =sysStockPriceWarnDao.selectOne(new QueryWrapper<SysStockPriceWarnEntity>().lambda()
                .eq(SysStockPriceWarnEntity::getId,id));
        if(entity!=null){
            List<SysStockProductAgeEntity> list= sysStockProductAgeDao.selectList(new QueryWrapper<SysStockProductAgeEntity>().lambda()
                    .eq(SysStockProductAgeEntity::getStockCode,entity.getProId()));
            if(list.size()>0){
                return false;
            }
            SysStockProductAgeEntity ageEntity=new SysStockProductAgeEntity();
            ageEntity.setId(entity.getId());
            ageEntity.setStockName(entity.getProName());
            ageEntity.setStockCode(entity.getProId());
            ageEntity.setCreateTime(new Date());
            ageEntity.setType("3");
            sysStockProductAgeDao.insert(ageEntity);
            return true;
        }
        return false;
    }


}